package com.softpo.gesturedetector;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ViewFlipper;

//案例手势识别的实例化到MainActivity，手势可以用到自定义View中（onTouchEvent）
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

    //识别手势，有一个类GestureDetector
    private GestureDetector mGestureDetector;

    private ViewFlipper mViewFlipper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        initViewFlipper();

        //1、实例化
        mGestureDetector = new GestureDetector(this,this);

        //2、添加双击事件的监听
        mGestureDetector.setOnDoubleTapListener(this);

        //3 ? 此时运行，mGestureDetector可以检测到屏幕点击事件
//        View.OnClickListener onClick = new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "自动触发点击事件", Toast.LENGTH_SHORT).show();
//            }
//        };
//
//        TextView textView = null;
//        textView.setOnClickListener(onClick);
    }

    //向控件ViewFlipper中添加图片
    private void initViewFlipper() {
        mViewFlipper.addView(getViewFromRes(R.mipmap.img001));
        mViewFlipper.addView(getViewFromRes(R.mipmap.img012));
        mViewFlipper.addView(getViewFromRes(R.mipmap.img017));
        mViewFlipper.addView(getViewFromRes(R.mipmap.img021));
        mViewFlipper.addView(getViewFromRes(R.mipmap.img030));
        mViewFlipper.addView(getViewFromRes(R.mipmap.img031));
        mViewFlipper.addView(getViewFromRes(R.mipmap.img033));
    }

    private View getViewFromRes(int id) {
        ImageView ret = new ImageView(this);

        ret.setScaleType(ImageView.ScaleType.CENTER_CROP);

        ret.setImageResource(id);
        return ret;
    }

    private void initView() {
        mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
    }

    //3 、将屏幕的触摸事件传给mGestureDetector（侦探）
    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        return super.onTouchEvent(event);//最高人民法院
        return mGestureDetector.onTouchEvent(event);//将屏幕上的触摸事件交给侦探
    }

    @Override
    public boolean onDown(MotionEvent e) {
        Log.d("flag", "----------------->onDown: 手指按下");
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.d("flag", "----------------->onShowPress: 手指按下长安之前触发");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d("flag", "----------------->onSingleTapUp: 手指单击抬起事件");
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d("flag", "----------------->onScroll: 手指按下并且滑动" );
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.d("flag", "----------------->onLongPress: 手指长按");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                           float velocityX, float velocityY) {
        Log.d("flag", "----------------->onFling: 手指滑动，手指屏幕，屏幕惯性滑动");
        //参数一二，MotionEvent，获取触摸事件的坐标，x轴坐标
        float x1 = e1.getX();
        float x2 = e2.getX();

        if(x1-x2>20){//e1在e2右边，手指开始的地方在右边---->向左滑动
            mViewFlipper.setInAnimation(this,R.anim.right_in);
            mViewFlipper.setOutAnimation(this,R.anim.left_out);
            mViewFlipper.showNext();
        }else if(x2-x1>20){
            mViewFlipper.setInAnimation(this,R.anim.left_in);
            mViewFlipper.setOutAnimation(this,R.anim.right_out);
            mViewFlipper.showPrevious();
        }
        return false;
    }


    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.d("flag", "----------------->onSingleTapConfirmed: 单击确定事件");
        //单击实现向左滑动
        mViewFlipper.setInAnimation(this,R.anim.right_in);
        mViewFlipper.setOutAnimation(this,R.anim.left_out);
        mViewFlipper.showNext();
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.d("flag", "----------------->onDoubleTap: 双击事件触发");
        //向右滑动
        mViewFlipper.setInAnimation(this,R.anim.left_in);
        mViewFlipper.setOutAnimation(this,R.anim.right_out);
        mViewFlipper.showPrevious();
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        Log.d("flag", "----------------->" +
                "onDoubleTapEvent: 双击之间发生的触摸事件：移动，抬起……");
        return false;
    }
}
